package com.ftwj.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftwj.config.resp.ResultMessage;
import com.ftwj.entity.SchoolRoomInfo;
import com.ftwj.entity.SchoolRoomSeatInfo;
import com.ftwj.service.SchoolRoomSeatService;
import com.ftwj.service.SchoolRoomService;
import lombok.extern.slf4j.Slf4j;
import org.mapstruct.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/school-room-seat")

public class SchoolRoomSeatController {
    @Autowired
    private SchoolRoomSeatService schoolRoomSeatService;

    @PostMapping("/Insert")
    public ResultMessage insertSchoolRoomSeatInfo(@RequestBody SchoolRoomSeatInfo schoolRoomSeatInfo) {
        int rows = schoolRoomSeatService.insertSchoolRoomSeatInfo(schoolRoomSeatInfo);
        return rows> 0 ? ResultMessage.success() : ResultMessage.error();
    }

    @PutMapping("/Update")
    public ResultMessage updateSchoolRoomSeatInfo(@RequestBody SchoolRoomSeatInfo schoolRoomSeatInfo) {
        int rows = schoolRoomSeatService.updateSchoolRoomSeatInfo(schoolRoomSeatInfo);
        return rows > 0 ?ResultMessage.success() : ResultMessage.error();
    }

    @PutMapping("/Update-OpenState")
    public ResultMessage updateSchoolRoomSeatOpenState(@RequestBody SchoolRoomSeatInfo schoolRoomSeatInfo) {
        int rows = schoolRoomSeatService.updateSchoolRoomSeatOpenState(schoolRoomSeatInfo);
        return rows > 0 ?ResultMessage.success() : ResultMessage.error();
    }

    @DeleteMapping("/Delete/{id}")
    public ResultMessage deleteSchoolRoomSeatInfo(@PathVariable("id") String id){
        int flag = schoolRoomSeatService.deleteSchoolRoomSeatInfo(id);
        return flag> 0 ? ResultMessage.success() : ResultMessage.error();
    }

    @GetMapping("/Select")
    public ResultMessage selectSchoolRoomSeatInfoAll(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size) {
        // 参数校验
        if (page < 1) page = 1;
        if (size < 1) size = 10;

        Page<Map<String, Object>> paging = new Page<>(page, size);
        IPage<Map<String, Object>> result = schoolRoomSeatService.selectSchoolRoomSeatInfoAll(paging);

        return ResultMessage.success(result);
    }

}
